package com.easyfun.healthmagicbox.comm.json;

import org.json.JSONException;
import org.json.JSONObject;

import com.easyfun.healthmagicbox.pojo.UserToPersons;

public class UserToPersonsJsonWrapper<T extends JsonWrapper> extends
		AbstractJsonWrapper<T,UserToPersons> {
	private UserToPersons mpp;

	public UserToPersonsJsonWrapper(UserToPersons pp) {
		mpp = pp;
	}

	public UserToPersonsJsonWrapper() {

	}

	public UserToPersons getUserToPersions()
	{
		return mpp;
	}
	@Override
	public void deserializeFromObj(JSONObject obj) throws JSONException {
		if (mpp==null)
    		mpp = new UserToPersons();
		mpp.setPersonID(obj.getString("personID"));
		mpp.setUsername(obj.getString("username"));
		mpp.setRetitle(obj.getString("retitle"));
		
		if (obj.isNull("id"))
		{
			mpp.setServerID("zzz");
		}else
		{
			mpp.setServerID(String.valueOf(obj.getInt("id")));
		}
		if (obj.isNull("modified"))
		{
			mpp.setModified("");
		}else
		{
			mpp.setModified(obj.getString("modified"));
		}
	}

	@Override
	public JSONObject serializeToObj() throws JSONException {
		JSONObject serializedObj = new JSONObject();
        serializedObj.put("personID", mpp.getPersonID());
        serializedObj.put("username", mpp.getUsername());
        //let server create the modified time
        //serializedObj.put("modified", mpp.getModified());
        serializedObj.put("retitle", mpp.getRetitle()==null?"自己":				
        	mpp.getRetitle());
 
        return serializedObj;
	}
}
